import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

# 初始化设置
plt.rcParams['font.sans-serif'] = ['KaiTi']  # 中文字体
plt.rcParams['mathtext.fontset'] = 'stix'     # 数学字体
plt.rcParams['axes.unicode_minus'] = False

# ==================== 数据准备阶段 ====================
# 设置随机种子保证结果可复现
np.random.seed(42)

# 生成特征数据X：100个样本，1个特征，取值范围[0,2)
X = 2 * np.random.rand(100, 1)

# 生成标签数据：真实模型为 y = 4 + 3X + 噪声
# 其中4是截距(bias term)，3是权重系数(weight)
# 噪声服从标准正态分布 N(0,1)
y = 4 + 3 * X + np.random.randn(100, 1)

# 划分训练集和测试集 (80%训练，20%测试)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# ==================== 岭回归模型 ====================
# 创建岭回归模型
# alpha: 正则化系数，控制模型复杂度
# solver: 优化算法，'auto'自动选择，'sag'适合大数据集
ridge_reg = Ridge(alpha=0.4, solver='auto', random_state=42)

# 训练模型
ridge_reg.fit(X_train, y_train)

# ==================== 模型评估 ====================
# 训练集预测
y_train_pred = ridge_reg.predict(X_train)
train_mse = mean_squared_error(y_train, y_train_pred)

# 测试集预测
y_test_pred = ridge_reg.predict(X_test)
test_mse = mean_squared_error(y_test, y_test_pred)

# ==================== 结果输出 ====================
print("===== 岭回归结果 =====")
print(f"截距项(w0): {ridge_reg.intercept_[0]:.4f}")
print(f"系数(w1): {ridge_reg.coef_[0][0]:.4f}")
print(f"训练集MSE: {train_mse:.4f}")
print(f"测试集MSE: {test_mse:.4f}")

# ==================== 可视化 ====================
plt.figure(figsize=(12, 6))

# 绘制训练数据
plt.scatter(X_train, y_train, color='blue', label='训练数据', alpha=0.6)

# 绘制测试数据
plt.scatter(X_test, y_test, color='green', label='测试数据', alpha=0.6)

# 绘制拟合线
X_plot = np.linspace(0, 2, 100).reshape(-1, 1)
y_plot = ridge_reg.predict(X_plot)
plt.plot(X_plot, y_plot, color='red', linewidth=2, label='岭回归拟合线')

plt.title('岭回归示例 (alpha=0.4)', fontsize=14)
plt.xlabel('X特征值', fontsize=12)
plt.ylabel('y标签值', fontsize=12)
plt.legend(fontsize=10)
plt.grid(True)
plt.show()


